<script>
	var provincetableoption={
			toolbar: '#provincetoolbar',//工具按钮用哪个容器
			clickToSelect: true,// 单击行即可以选中
			singleSelect: true, //设置true禁止多选
			sidePagination: "client",           //分页方式：client客户端分页，server服务端分页（*）
            showPaginationSwitch: true,
			showExport: true,
			exportDataType: 'all',
			exportTypes: ['doc', 'excel'], //导出文件类型
			showToggle: true,  //是否显示视图切换按钮
			showRefresh: true,// 是否显示刷新按钮
			showColumns: true,//是否显示所有的列
			search: true, // 开启搜索功能
			pagination: true,// 开启分页功能
			sortName: "pid", // 设置默认排序为 name
			columns: [{				field: "checked",				checkbox: true				}, {				field: "isused",				title: "是否使用",				align: "center",				formatter : function (value, row, index) { switch(value) {case 1 :return "是"; default : return "否"}},				valign: "middle"				}, {				field: "province",				title: "省名",				align: "center",				valign: "middle"				}, {				field: "pid",				title: "省id",				visible: false,				align: "center",				valign: "middle"			}],
			onlClickCell: function (field,value,row) {
					row.checked=true;
			},
			onCheck: function (row) {
				if (actions.includes("183")) {
					console.log(row)
					switch(row.isused) {
						case 0:$("#185").text("显示");break;
						case 1:$("#185").text("隐藏");break;
						default:$("#185").text("显示");break;
					}
					provinceEditTable(row);
				}
			}
		}

	$(function() {
		checkaction(new Array("182","183","185","186","187"));

		$("#provinceQueryTable").bootstrapTable(provincetableoption);
		$("#provinceQueryTable").bootstrapTable('showLoading');

		provinceQueryTable();
		$(".selectpicker").selectpicker("refresh");
		$("#provincemyModal").on("shown.bs.modal",function(e){ if ($("#txt_pid").attr("disabled")) { $("#txt_SaveModal").focus() } else {$("#txt_pid").focus()}; });
	});
	
	function provinceAddTable() {//新增
		$("#txt_province").val("");		$("#txt_pid").val("");		$("#txt_pid").attr("disabled",false);
        $("#provincemyModalLabel").html("新增");
        
	};
	
	function provinceEditTable(row) {
		if (row==null) {
			if ($("#provinceQueryTable").bootstrapTable('getSelections').length == 0) {
				alert("请先选择要修改的数据!");
				return;
			};
			row=$("#provinceQueryTable").bootstrapTable('getSelections')[0];
		}
		if (row.province!=null) { $("#txt_province").val(row.province) } else { $("#txt_province").val("")};		if (row.pid!=null) { $("#txt_pid").val(row.pid) } else { $("#txt_pid").val("")};		$("#txt_pid").attr("disabled",true);
        $("#provincemyModalLabel").html("修改");
        
	};
	
	function provinceCommitTable() {
		var provincecommitdata={
			province:$("#txt_province").val(),			pid:$("#txt_pid").val(),			token:token,
		};
		if ($("#provincemyModalLabel").html()=="新增") {
			ajaxarr([{
				url:"sysinfo/province_add",
				title:"新增数据失败！",
				data:provincecommitdata,
				func:function(data) {
					alert("新增数据成功！id："+data.info.id);
					provinceQueryTable(data.info.id);
					provinceAddTable();
				}
			}]);
		} else {
			ajaxarr([{
				url:"sysinfo/province_edit",
				title:"修改数据失败！",
				data:provincecommitdata,
				func:function(data) {
					alert("修改数据成功！");
					provinceQueryTable(provincecommitdata.pid);
					
				}
			}]);		
		};
	};
	
	function provinceDelTable() {
        var row = $("#provinceQueryTable").bootstrapTable('getSelections')[0];
        if(row == null) {
            alert("请先选择要删除的数据!");
            return;
        };       
		if(confirm( "您真的确定要删除吗？") == true) {
			ajaxarr([{
				url: "sysinfo/province_del" ,
				title: "删除数据失败！",
				data:{token:token,pid : row.pid },
				func: function(data) {
					alert("删除成功！");
					provinceQueryTable();
				}
			}]);
		}
	};
	
	function provinceUndelTable() {
        var row = $("#provinceQueryTable").bootstrapTable('getSelections')[0];
        if(row == null) {
            alert("请先选择要恢复的数据!");
            return;
        };       
		if(confirm( "您真的确定要恢复吗？") == true) {
			ajaxarr([{
				url: "sysinfo/province_undel" ,
				title: "恢复数据失败！",
				data:{token:token,pid : row.pid },
				func: function(data) {
					alert("恢复成功！");
					provinceQueryTable();
				}
			}]);
		}
	}


	function provinceQueryTable(id) {
		$("#provinceQueryTable").bootstrapTable('showLoading');
		ajaxarr([{
				url:"sysinfo/province_query",
				title:"获取省信息失败，请刷新！",
				data:{token:token},
				func: function (data) {
					if (data.info.total==0) { $("#provinceQueryTable").bootstrapTable("destroy").bootstrapTable(provincetableoption); } 
					else {	
						$("#provinceQueryTable").bootstrapTable('load', data.info.rows); 
						if (id) {
							for (var i=data.info.total;i>0;i--) {
								if (data.info.rows[i-1].pid==id) {
									$("#provinceQueryTable").bootstrapTable('selectPage', Math.ceil(i/$("#provinceQueryTable").bootstrapTable('getOptions').pageSize));
									$("#provinceQueryTable").bootstrapTable('checkBy',{field:'pid',values:[data.info.rows[i-1].pid]})
									break;
								};							
							}
						} else {
							$("#provinceQueryTable").bootstrapTable('checkBy',{field:'pid',values:[data.info.rows[0].pid]})
							provinceEditTable(data.info.rows[0]);
						}
					};
					$("#provinceQueryTable").bootstrapTable('hideLoading');
				}
		}]);		
	};
</script>
<table id="provinceQueryTable" class='table-striped'></table>
<div id="provincetoolbar" class="btn-group">
	<button id="182" type="button" style="display:none;" class="btn btn-default" onclick="provinceAddTable()"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增</button>
	<button id="183" type="button" style="display:none;" class="btn btn-default" onclick="provinceEditTable()"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改</button>
	<button id="185" type="button" style="display:none;" class="btn btn-default" onclick="provinceDelTable()"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除</button>
	<button id="187" type="button" style="display:none;" class="btn btn-default" onclick="provinceUndelTable()"><span class="glyphicon glyphico-ok" aria-hidden="true"></span>恢复</button>
	<button id="186" type="button" style="display:none;" class="btn btn-default" onclick="provinceQueryTable()"><span class="glyphicon glyphico-query" aria-hidden="true"></span>查询</button>
</div>
<div class="row" id="provincemyModal" tabindex="-1" >
	<div class="col-12" >
		<div class="modal-content" >
			<div class="modal-header">
				<h4 class="modal-title" id="provincemyModalLabel">新增</h4>
				
			</div>
			<div class="modal-body form-inline">
				<label for="txt_province" class="col-lg-3 text-center">省名</label><input id="txt_province" class="form-control col-lg-3" onkeypress="{ if (event.keyCode==13) {txt_pid.focus()}}" type="text" placeholder="省名"/>				<label for="txt_pid" class="col-lg-3 text-center">省id</label><input id="txt_pid" class="form-control col-lg-3" disabled="disabled" onkeypress="{ if (event.keyCode==13) {SaveModal.focus()}}" type="text" placeholder="省id"/>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal" onclick="provinceAddTable()"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>清空</button>
				<button type="button" class="btn btn-primary" id="provinceSaveModal" onclick="provinceCommitTable()"><span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>保存</button>
			</div>
		</div>
	</div>
</div>
